package com.ruoyi.web.controller.system.King;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.KingTemporary.*;
import com.ruoyi.system.service.*;
import com.ruoyi.system.service.King.IKingCanService;
import com.ruoyi.web.until.AssembleUtil;
import com.ruoyi.web.until.InvokeHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;

import static org.aspectj.bridge.MessageUtil.fail;

/**
 * 用户信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/King/can")
public class KingCanController extends BaseController
{
    @Autowired
    private IKingCanService iKingCanService;
//
    @Autowired
    private IKingBdCangService iKingBdCangService;

    @Autowired
    private IKingBdCangwService iKingBdCangwService;

    @Autowired
    private IKingBdWuService iKingBdWuService;

    @Autowired
    private IKingBdOrganizationService iKingBdOrganizationService;

    @Autowired
    private IKingBdUserService iKingBdUserService;

    /**
     * 获取金蝶仓库
     */
    @CrossOrigin
    @PostMapping("/getAllcode")
    public void getInstance() throws Exception {
        List<String> list = iKingCanService.getAllCang();
        if(list.size()>0) {
            for(int i=0;i<list.size();i++){
                Fanhui fanhui = getCan(list.get(i));
                KingBdCang kingBdCang = new KingBdCang();
                kingBdCang.setfId(String.valueOf(fanhui.getResult().getResult().getId()));
                kingBdCang.setName(fanhui.getResult().getResult().getName().get(0).getValue());
                kingBdCang.setfCode(fanhui.getResult().getResult().getNumber());
                kingBdCang.setUserCode(fanhui.getResult().getResult().getUseOrgId().getNumber());
                kingBdCang.setUserName(fanhui.getResult().getResult().getUseOrgId().getMultiLanguageText().get(0).getName());
                if(fanhui.getResult().getResult().getStockFlexItem().size()>0){
                    if(fanhui.getResult().getResult().getStockFlexItem().get(0).getFlexId()!=null) {
                        List<Fanhui.ResultBeanX.ResultBean.StockFlexItemBean.StockFlexDetailBean> list1 = fanhui.getResult().getResult().getStockFlexItem().get(0).getStockFlexDetail();
                        for (Fanhui.ResultBeanX.ResultBean.StockFlexItemBean.StockFlexDetailBean stockFlexDetailBean : list1) {
                            KingBdCangw kingBdCangw = new KingBdCangw();
                            kingBdCangw.setfCode(stockFlexDetailBean.getFlexEntryId().getNumber());
                            kingBdCangw.setfId(String.valueOf(stockFlexDetailBean.getId()));
                            kingBdCangw.setName(stockFlexDetailBean.getFlexEntryId().getName().get(0).getValue());
                            kingBdCangw.setsCode(kingBdCang.getfCode());
                            if(iKingBdCangwService.getNum(kingBdCangw.getfCode())==0) {
                                iKingBdCangwService.insertKingBdCangw(kingBdCangw);
                            }
                        }
                    }
                }
                if(iKingBdCangService.getNum(kingBdCang.getfId())==0) {
                    iKingBdCangService.insertKingBdCang(kingBdCang);
                }
            }
        }

    }

    /**
     * 获取金蝶物料
     */
    @CrossOrigin
    @PostMapping("/getWu")
    public void getWu() throws Exception {
        List<KingWu> list = iKingCanService.getAllWu();
        if(list.size()>0){
            for(KingWu kingWu:list){
                KingBdWu kingBdWu = new KingBdWu();
                BeanUtils.copyProperties(kingWu,kingBdWu);
                if(iKingBdWuService.getNum(kingBdWu.getfId())==0){
                    kingBdWu.setfGroupCode(kingWu.getfClassCode());
                    kingBdWu.setfGroupName(kingWu.getfClassName());
                    KingBdWl kingBdWl = getBdwl(kingBdWu.getfId());

                    kingBdWu.setfUnit(kingBdWl.getResult().getResult().getMaterialBase().get(0).getBaseUnitId().getNumber());
                    if(kingBdWl.getResult().getResult().getMaterialStock().get(0)!=null) {
                        if (kingBdWl.getResult().getResult().getMaterialStock().get(0).getAuxUnitID() != null){
                            if (kingBdWl.getResult().getResult().getMaterialStock().get(0).getAuxUnitID().getNumber() != null) {
                                kingBdWu.setfAssis(kingBdWl.getResult().getResult().getMaterialStock().get(0).getAuxUnitID().getNumber());
                            }
                        }
                    }
                    iKingBdWuService.insertKingBdWu(kingBdWu);
                }
            }
        }
    }

    @CrossOrigin
    @PostMapping("/upWuliao")
    public void upWuliao() throws Exception {
       List<KingBdWu> list = iKingBdWuService.getNameNull();
       if(list.size()>0) {
           for (KingBdWu kingBdWu :list){
               KingBdWl kingBdWl = getBdwl(kingBdWu.getfId());
               kingBdWu.setfName(kingBdWl.getResult().getResult().getMultiLanguageText().get(0).getName());
               if(kingBdWl.getResult().getResult().getMultiLanguageText().get(0).getSpecification()!=null){
                   kingBdWu.setRemarks(kingBdWl.getResult().getResult().getMultiLanguageText().get(0).getSpecification());
               }
               iKingBdWuService.updateKingBdWu(kingBdWu);

           }
        }
    }

    /**
     * 获取金蝶物料对应的使用组织详细信息
     */
    @CrossOrigin
    @PostMapping("/getOrgan")
    public void getOrgan() throws Exception {
        List<String> list = iKingBdWuService.getUserList();
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                if(iKingBdOrganizationService.getNum(list.get(i))==0){
                    KingBdOrganization kingBdOrganization = new KingBdOrganization();
                    Organ organ = getOrgan(list.get(i));
                    kingBdOrganization.setfId(list.get(i));
                    kingBdOrganization.setfCode(organ.getResult().getResult().getNumber());
                    kingBdOrganization.setfName(organ.getResult().getResult().getName().get(0).getValue());
                    iKingBdOrganizationService.insertKingBdOrganization(kingBdOrganization);
                }
            }
        }
    }

    /**
     * 获取金蝶物料员工信息
     */
    @CrossOrigin
    @PostMapping("/getStaff")
    public void getStaff() throws Exception {
        List<Map<String,String>> list = iKingCanService.getAllStaff();
        if(list.size()>0){
            for(Map<String,String> map:list){
                KingBdUser kingBdUser = new KingBdUser();
                kingBdUser.setfId(String.valueOf(map.get("fId")));
                kingBdUser.setfCode(String.valueOf(map.get("fCode")));
                Staff staff = getStaff(String.valueOf(map.get("fId")));
                kingBdUser.setName(staff.getResult().getResult().getName().get(0).getValue());
                kingBdUser.setCompanyId(staff.getResult().getResult().getCreateOrgId().getName().get(0).getValue());
                kingBdUser.setCompany(staff.getResult().getResult().getCreateOrgId().getNumber());
                kingBdUser.setPhone(staff.getResult().getResult().getMobile());
                if(staff.getResult().getResult().getStaffId()!=null) {
                    kingBdUser.setPostId(staff.getResult().getResult().getStaffId().getPosition().getNumber());
                    kingBdUser.setPostName(staff.getResult().getResult().getStaffId().getPosition().getName().get(0).getValue());
                }
                if(iKingBdUserService.getNum(String.valueOf(map.get("fId")))==0){
                    iKingBdUserService.insertKingBdUser(kingBdUser);
                }
            }
        }
    }

    public static Fanhui getCan(String ids) throws Exception {
        Fanhui fanhui = new Fanhui();
       if(StringUtils.isNotEmpty(ids)){
           JSONObject jsonObject2 = new JSONObject();
           jsonObject2.put("CreateOrgId",0);
           jsonObject2.put("Number","");
           jsonObject2.put("Id",ids);
           com.alibaba.fastjson.JSONObject object = InvokeHelper.Invoke2("View","BD_STOCK",jsonObject2.toString(),InvokeHelper.getCook());
           fanhui = JSON.toJavaObject(object,Fanhui.class);
       }
       return fanhui;
    }



    public static Organ getOrgan(String ids) throws Exception {
        Organ organ = new Organ();
        if(StringUtils.isNotEmpty(ids)){
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("CreateOrgId",0);
            jsonObject2.put("Number","");
            jsonObject2.put("Id",ids);
            com.alibaba.fastjson.JSONObject object = InvokeHelper.Invoke2("View","ORG_Organizations",jsonObject2.toString(),InvokeHelper.getCook());
            organ = JSON.toJavaObject(object,Organ.class);
        }
        return organ;
    }

    public static KingBdWl getBdwl(String ids) throws Exception {
        KingBdWl kingBdWl = new KingBdWl();
        if(StringUtils.isNotEmpty(ids)){
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("CreateOrgId",0);
            jsonObject2.put("Number","");
            jsonObject2.put("Id",ids);
            com.alibaba.fastjson.JSONObject object = InvokeHelper.Invoke2("View","BD_MATERIAL",jsonObject2.toString(),"9ac4feab-5636-4e0b-9b16-9380c6450af6");
            kingBdWl = JSON.toJavaObject(object,KingBdWl.class);
        }
        return kingBdWl;
    }

    public static Staff getStaff(String ids) throws Exception {
        Staff staff = new Staff();
        if(StringUtils.isNotEmpty(ids)){
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("CreateOrgId",0);
            jsonObject2.put("Number","");
            jsonObject2.put("Id",ids);
            com.alibaba.fastjson.JSONObject object = InvokeHelper.Invoke2("View","BD_Empinfo",jsonObject2.toString(),"dc499eb6-f369-48fb-a14f-922d17469ce1");
            staff = JSON.toJavaObject(object,Staff.class);
        }
        return staff;
    }




}
